package com.zyzo.demo.appengine.client.customwidget.user.CustomerUserCreator;

import java.io.IOException;
import java.util.Date;

import org.restlet.client.Request;
import org.restlet.client.Response;
import org.restlet.client.Uniform;
import org.restlet.client.data.ChallengeScheme;
import org.restlet.client.data.MediaType;
import org.restlet.client.ext.json.JsonRepresentation;
import org.restlet.client.resource.ClientResource;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.zyzo.demo.appengine.client.Json2Model.Converter;
import com.zyzo.demo.appengine.model.User;

public class CustomerUserCreator extends Composite 
{	
	  @UiField TextArea addressBox;
	  @UiField TextBox mailBox;
	  @UiField TextBox firstNameBox;
	  @UiField TextBox lastNameBox;
	  @UiField TextBox passwordBox;
	  @UiField TextBox phoneBox;
	  
	  @UiField Button createButton;
	  @UiField Button updateButton;
	  
	private static CustomerUserCreatorUiBinder uiBinder = GWT
			.create(CustomerUserCreatorUiBinder.class);

	interface CustomerUserCreatorUiBinder extends
			UiBinder<Widget, CustomerUserCreator> {
	}

	public CustomerUserCreator() {
		initWidget(uiBinder.createAndBindUi(this));
	}



	public CustomerUserCreator(String firstName) {
		initWidget(uiBinder.createAndBindUi(this));
		
		createButton.setText("create");
		updateButton.setText("update");
	}
 

	

	@UiHandler("createButton")
	void onCreateButtonClick(ClickEvent event) 
	{
		User userPost = new User();
		long r = (new Date()).getTime() ;
		
		userPost.setId( r );
		userPost.setEmail( mailBox.getValue() );
		userPost.setFirstname( firstNameBox.getValue() );
		userPost.setLastname( lastNameBox.getValue() );
		userPost.setPassword( passwordBox.getValue() );
		userPost.setPhone(phoneBox.getValue() );
		userPost.setSubscriptionDate( new Date() );
		
		ClientResource cr = new ClientResource("/rest/admin/user");
		cr.setChallengeResponse(ChallengeScheme.HTTP_BASIC, "admin@mail.com", "admin");
		cr.setOnResponse
		(
			new Uniform() 
			{
		        public void handle(Request request, Response response) 
		        {
		        	try 
		        	{
						Window.alert( response.getEntity().getText() );
						
					} catch (IOException e) {
						Window.alert(e.getMessage());
					}
		        }
			}
		);
		
		JSONObject jsonvalue =  Converter.getInstance().userToJson(userPost) ;
		cr.put( new JsonRepresentation( MediaType.APPLICATION_JSON, jsonvalue ) );   	
	}
	
	
	
	
}
